module.exports = (EdtApi) => {
    "use strict";
    let chatId = "", count = 0, badges;
    function isGroup() {
        let el = document.querySelectorAll('.gvThreadItem-selected .gvThreadItem-avatarParticipants .avatar.ng-star-inserted');
        return el.length > 1;
    }
    async function getChatId(times = 0) {
        let headerEl = document.querySelector(".header>.header-content div.primary-text");
        let id = headerEl?.textContent || "";
        if (!id) {
            headerEl = document.querySelector("mat-chip-row .chip-name.ng-star-inserted");
            id = headerEl?.textContent || "";
        }
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    return {
        config: {
            friendList: 'gv-conversation-list[selected-item-id],#contact-list,.gvOmnibar-root.gvPageRoot,.md-virtual-repeat-scroller,div.gvThreadDetails-fullViewButton,.gvMessagingView-conversationList,.cdk-overlay-pane #contact-list,gv-thread-list-ng2',
            chat: 'gv-thread-details.gvMessagingView-threadDetailsWrap.gvMessagingView-threadDetailsActive',
            input: "div.gvThreadDetails-messageEntryContainer div.message-input-container>textarea.message-input",
            msgBox: 'gv-text-message-list',
            inMsg: 'ul.list>li div.message-row:not(.outgoing)>div.subject-content-container>gv-annotation.content:not(.trans-inited),div.gvTextMessageList-incoming gv-message-item gv-annotation.gvMessageItem-content:not(.trans-inited)',
            outMsg: 'ul.list>li div.message-row.outgoing>div.subject-content-container>gv-annotation.content:not(.trans-inited),div.gvTextMessageList-outgoing gv-message-item gv-annotation.gvMessageItem-content:not(.trans-inited)',
            sendBtn: 'div.gvThreadDetails-messageEntryContainer div.button-container.dense-icon-button-container>button[mattooltipposition="before"]',
            setting: "div.message-input-container",
            tips: {
                in: 'div.gvMessageListHeader-messageHeader>div[layout-align="center"],gv-thread-details-header .header-content',
                out: 'div.gvThreadDetails-messageEntryContainer',
            },
            placeholder: {
                selector: "div.gvThreadDetails-messageEntryContainer div.message-input-container>textarea.message-input",
                attr: "placeholder"
            },
            extra: {//其他会触发对话框变动的情况处理
                selector: "div.gvThreadDetails-fullViewButton",
                level: 5,
                delay: 200
            },
            inputType: "input",
            sendType: "keyup",
            autoCount: 20
        },
        getUnread() {
            count = 0;
            badges = document.querySelectorAll('mat-sidenav-content a[gv-test-id="sidenav-messages"] span.navItemBadge.ng-star-inserted');
            badges?.forEach(badge => {
                count += EdtApi.safeParseInt(badge.textContent);
            });
            return count;
        },
        getChatInfo() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        getText(msgBox) {
            if (!msgBox) return '';
            let text = '';
            try {
                if (!msgBox.querySelector('img.element.medium')) {
                    text = msgBox.textContent;
                } else {
                    msgBox.childNodes?.forEach(child => {
                        if (child.nodeName == '#text') {
                            text += child.textContent;
                        } else if (child.nodeName == 'IMG') {
                            text += child.getAttribute("alt") || "";
                        } else if (child.nodeName == "BR") {
                            text += '\n';
                        }
                    });
                }
            } catch (error) {
                console.error(error);
            }
            return text;
        },
        getIptMsg(inputEl) {
            if (!inputEl) return "";
            return inputEl.value;
        },
        getUserInfo() {
            try {
                let userEl = document.querySelector('header a.gb_Za[role="button"]');
                let avatarEl = userEl?.querySelector("img");
                let avatar = avatarEl?.getAttribute("src") || "";
                let userInfo = userEl?.getAttribute("aria-label") || "";
                let userName = "";
                let account = "";
                if (userInfo?.includes("：")) userInfo = userInfo.split("：")[1];
                if (userInfo?.includes(":")) userInfo = userInfo.split(":")[1];
                if (userInfo?.includes("(")) {
                    let strs = userInfo.split("(");
                    userName = strs[0]?.trim();
                    account = strs[1]?.trim()?.replace(")", "");
                }
                return {
                    avatar,
                    userName,
                    account
                };
            } catch (error) {
                console.error(error);
                return null;
            }
        }
    }
};